Skip to content

fix(control-plane,turn-driver): preserve selected-scope resume across epochs and bind physical native replay calls - #8942

Open
loopx-agent wants to merge 2 commits into
mainfrom
codex/c1-standalone-core-20261118
Open

loopx-agent wants to merge 2 commits into
mainfrom
codex/c1-standalone-core-20261118

Conversation

@loopx-agent

@loopx-agent loopx-agent commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Standalone C1 core slice extracted directly against main (0514512ee7). Refs #8266.

Goal & Root-Cause Context

Mega-PR #8266 (301 files, +27,305 / -4,938 LOC across 537 commits) accumulated ledger churn (+9,081 net lines in docs/architecture/), standalone test permutations (+8,640 net lines), and parallel v0/v1 compatibility glue across CLI adapters. Rather than continuing to stack onto conflicting #8266 (stacked PR #8882 closed as superseded), this PR extracts one self-contained, single-owner C1 native replay & epoch-continuity slice (5 files, +599 / -171 LOC, 0 ledger edits, conflict-free MERGEABLE against main and active PRs):

  1. Selected-scope resume across epoch rotation (loopx/control_plane/quota/native_context_lifecycle.ts, +25 / -13):
    When SessionStart(source="resume") observes a failed full-capture replay (candidate.native_resume invalidation), rotate the capture file epoch via invalidate(), while advancing read_scope and preserving consumed_pointers, confirmed_read_sections, and selected_resume when independently verified selected_resume sections remain current in the rotated epoch. Clear them if omitted or contradicted.
  2. Turn-scoped physical replay binding, multi-page selected coverage, and deduplicated IO helpers (loopx/control_plane/turn_driver/codex_context_history.py, +278 / -127; codex_context.py, +4 / -3):
    • Replace tail string-scan heuristics (_reader_start + Counter) with physical Turn-bounded reader anchoring (_reader_tail, _turn_boundary, _native_reader) verifying CommandExecution completion/turn identity and unique open Code Mode custom_tool_call (exec) parents without scanning older history prefixes.
    • Support multi-page & out-of-order selected-read coverage keyed by verified view digest (_covers_bytes, selections), observe_completed_native_reread (_MAX_COMPLETE_REREADS = 32), and shared helper consolidation (_REPLAY_ERRORS, _read_receipt, _matching_resume_session, _verified_replay_capture, _same_replay_source).
  3. Standalone verification coverage (tests/control_plane_ts/context_delivery.test.ts, +35 / -1; tests/control_plane/test_codex_context_resume.py, +257 / -27):
    • Add direct TypeScript unit coverage for updateNativeContextLifecycle epoch-rotated selected_resume retention/clearing.
    • Cover direct, batched, multi-page, out-of-order, missing-hook/lost-page, cross-turn reread, unobserved duplicate copy, and repeated-resume epoch survival scenarios in test_codex_context_resume.py.

Verification (run against committed 0ba725fef26d1f8fa3c5f2596f0d5245388da256)

  • uv run --extra test python -m ruff check loopx/control_plane/turn_driver/codex_context_history.py loopx/control_plane/turn_driver/codex_context.py tests/control_plane/test_codex_context_resume.py -> All checks passed!
  • uv run --extra test python -m mypy --follow-imports=silent loopx/control_plane/turn_driver/codex_context_history.py -> Success: no issues found in 1 source file; uv run --extra test python -m mypy -> Success: no issues found in 17 source files
  • npm run typecheck:control-plane (tsc --noEmit) -> exit 0
  • node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/context_*.test.ts tests/control_plane_ts/progressive_context.test.ts tests/control_plane_ts/turn*.test.ts -> 343 passed, 0 failed, 0 skipped
  • uv run --extra test python -m pytest -n auto -q --no-cov -rs tests/control_plane/test_codex_context_resume.py -> 145 passed in 10.49s (0 skipped)
  • uv run --extra test python -m pytest -n auto -q --no-cov tests/control_plane/test_codex_context_resume.py tests/control_plane/test_codex_context_lifecycle.py tests/control_plane/test_context_delivery_cli.py tests/control_plane/test_turn_host_context_delivery.py tests/control_plane/test_codex_context_boundary.py tests/architecture/test_posix_flag_portability.py tests/architecture/test_content_digest_single_owner.py tests/architecture/test_semantic_vocabulary_drift.py tests/architecture/test_rfc_status_index.py -> 799 passed, 24 skipped in 147.16s
  • uv run --extra test python scripts/check_rfc_bilingual.py --base origin/main --head HEAD -> RFC bilingual diff check passed.
  • uv run --extra test python examples/control_plane/control-plane-maintainability-ratchet-smoke.py -> control-plane-maintainability-ratchet: ok (unreviewed: 0, magnitude_regressions: 0)
  • Isolated HOME uv run --extra test loopx check --scan-path <all 5 changed paths> -> ok: True, errors=0, public boundary scan clean: 5 files
  • Isolated HOME uv run --extra test loopx canary premerge --from-git-diff --git-diff-base origin/main -> status: passed, merge_gate_passed: true (direct_checks: 6/6, catalog_canaries: 5/5 failures=0, risk_profile_smokes: 8/8 failures=0)

Rebase onto origin/main (648d06db54) & [P2] Streaming Scan Fix (dca761a9e5)

  • Rebased conflict-free onto origin/main 648d06db54 (includes #8881, #8956, #9000, #9004; fixes stale base lint error).
  • Resolved gpt-6.2-sol [P2] review on _native_reader: replaced full-line multi-MiB buffering in _reader_tail with O(64 KiB) _reader_spans + _JsonSliceCursor / _scan_turn_prefix_record using bounded {1,64} regex span scanning (_JSON_STR_SPAN), streaming oversized (>16 MiB) earlier same-turn records (background_event / closed custom_tool_call_output) to _turn_boundary (task_started) at < 2 MiB peak memory (147 KiB micro-benchmark peak on 17 MiB escaped JSON) while still rejecting active >16 MiB reads, malformed JSON/control chars, and duplicate concurrently open Code Mode exec parents.
  • Exact-head dca761a9e5 Verification:
    • tests/control_plane_ts/context_delivery.test.ts + quota_settlement_plan.test.ts: 49 passed, 0 failed (tsc --noEmit exit 0)
    • tests/control_plane/test_codex_context_resume.py -k large_earlier_record: 14 passed, 145 deselected in 16.07s
    • Core Python suite (test_codex_context_resume.py, test_codex_context_lifecycle.py, test_top_level_module_budget.py, test_goal_mode_mcp_settlement.py): 344 passed in 88.17s (0 failed, 0 skipped)
    • 10-suite C1 sweep: 985 passed, 14 skipped, 4 failed (the 4 failures at test_context_first_read.py:874 and test_saved_context_project.py:711 reproduce identically on unmodified origin/main@648d06db54)
    • uv run --extra test ruff check ..., mypy --strict --follow-imports=silent, uv run --extra test mypy (17 files): 0 issues
    • uv run --extra test loopx canary premerge --tier quick --from-git-diff --git-diff-base origin/main: ok=true (6/6 direct, 3/3 selected canaries failures=0)
    • uv run --extra test loopx check --scan-path ...: ok=true (errors=0, public boundary clean across 5 files)
    • Isolated SQLite + real subprocess CLI host matrix (quota should-run, context-read, context-hook, refresh-state, spend-slot): verified 17 MiB same-turn Code Mode selected_resume, unchanged-SHA previously_delivered_fields hit, changed-SHA safety, PreCompact recovery with 0 extra admissions and single debit (1 -> 2), duplicate spend-slot blocked, and wrong-model / missing-page (2/3) / stale-SHA rejection.

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent | self_reported | Gemini | Google DeepMind

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

动机 / Motivation

运行长程多轮会话并经 session_start resume 或选区重放恢复上下文的 Codex App/CLI Agent 与 turn_driver 维护者。
此前当选区恢复后发生失败的完整重放并触发 epoch 轮换时,原有逻辑在清理 candidate 的同时把旧 recovery 文件与 selected_resume 的选区状态一并清空,迫使 Agent 在恢复回合退回重新全量读盘;本次把该逻辑从 300 文件巨无霸 PR 中干净摘出为 5 文件聚焦切片(+599/-171,零旧兼容账本膨胀),跨 epoch 保留选区重放锚点并直接绑定物理 native replay 调用。
在 native_context_lifecycle.ts 精确以 readScopeCurrent && selectedResumeCurrent 在 epoch 轮换时迁延 selected_resume 及其已校验 sections,并在 codex_context_history.py 补全 _replayed_output_groups / observe_completed_native_reread 物理捕获校验。
非目标边界:本 PR 为独立的 C1 上下文恢复核心切片,不混入无关模块搬迁、UI 改造或历史 ledger 回写。

独立拆出 C1 核心切片避免了超大混合分支的持续合入冲突与审评僵局,让长程断点恢复在 epoch 轮换下保持高命中、高可信。

改动思路 / Approach

将选区恢复的跨 epoch 保留判定严格收口进 TypeScript 单一生命周期所有者 updateNativeContextLifecycle,同时由 Python codex_context_history 仅负责物理 JSONL/字节区间与摘要核验,两层职责清晰、无双重状态机。
当前 PR 边界:仅涵盖 C1 selected_resume 跨 epoch 保留与物理 native replay 锚定(5 files: native_context_lifecycle.ts, codex_context.py, codex_context_history.py 与对应 TS/Python 测试,+599/-171)。

  • 状态转移不变量:仅当 readScopeCurrent 且 selectedResumeCurrent 同时成立时才允许跨 context_epoch 迁延已被前序字节摘要核验过的 retained sections;若普通失效但无有效复核选区,则维持清空 consumed_pointers 与 confirmed_read_sections,绝不越权免读。
  • 物理证据不变量:_verified_replay_capture 与 observe_completed_native_reread 强制依据会话 rollout JSONL 偏移量、字节覆盖区间与 SHA 摘要核验真实完整重读和选区重放,防止伪造读记录绕过门禁。

具体改动 / Changes

依据规范 docs/architecture/rfcs/controller-executor-unified-contract-v0.md(修订 ef099ae3ac3c08054b9d928fff48aefe2615b950)核对核心条款:

  • C1-SELECTED-RESUME-EPOCH:选区恢复跨 epoch 仅保留经严格摘要比对的字段并同步更新 selected_resume.epoch(由 tests/control_plane_ts/context_delivery.test.ts 36/36 PASS 验证)。
  • C1-PHYSICAL-REPLAY-ANCHOR:物理 native replay 校验完整字节范围与页摘要覆盖(由 tests/control_plane/test_codex_context_resume.py 与 budget 测试共 151 passed 验证)。

关键代码讲解

  1. loopx/control_plane/quota/native_context_lifecycle.ts:68(updateNativeContextLifecycle):计算 readScopeCurrent 与 selectedResumeCurrent,在 candidate?.native_resume 失败触发 epoch 轮换后,将经 digest 核验的 retained sections 与新 context_epoch 一并写入 state.read_scope 与 state.selected_resume。
  2. loopx/control_plane/turn_driver/codex_context_history.py:182(_verified_replay_capture / _replayed_output_groups / observe_completed_native_reread):重整重放捕捉切片逻辑,核对 rollout 字节偏移与完整覆盖区间,补齐 PostToolUse 完整重读回执观测。
  3. loopx/control_plane/turn_driver/codex_context.py:417(codex_context_hook):从分页输出过滤出全量页面并串联 checkpoint_native_read 与 observe_completed_native_reread。

对主干的风险 / Risks

变更收敛在 5 个核心与测试文件,无新增顶层 Python 模块,无新持久化配置文件或外部依赖。潜逃风险主要是重放日志截断或损坏,此时 _covers_bytes 与 _matching_replay_interval 会 fail-closed 回退为无效重放并要求重新完整读盘,不放行半截状态。远端 Actions kernel-static-checks 当前失败系基底 origin/main 上 tests/control_plane/test_contract_health.py:10 F401(本 PR 未改该文件)。

验证项 级别 结果 实跑输出(exact head f3620ddf7c9742060b8044fb1de7177e7f96a4e2)
pytest -q tests/control_plane/test_codex_context_resume.py tests/architecture/test_top_level_module_budget.py required PASS 151 passed in 21.23s
node --experimental-strip-types --test tests/control_plane_ts/context_delivery.test.ts & tsc --noEmit required PASS 36 passed, 0 failed; tsc rc=0
ruff check loopx/control_plane/turn_driver/codex_context.py loopx/control_plane/turn_driver/codex_context_history.py tests/control_plane/test_codex_context_resume.py required PASS All checks passed!
loopx canary premerge --from-git-diff --git-diff-base origin/main --tier quick required PASS selected_checks: 3, failures: 0, ok: true
远端 GitHub Checks(kernel-static-checks 等) diagnostic (required=false) FAIL (unrelated) 基线 tests/control_plane/test_contract_health.py:10 F401 预存故障

我的整体评价 / Verdict

对精确 head f3620ddf7c9742060b8044fb1de7177e7f96a4e2 的评价:用户体验 preserved / 视觉维度 not_applicable(仅涉及 turn_driver 上下文生命周期与 TS 状态转移,未改动任何前端 UI、CSS 或可渲染界面(inspected: loopx/control_plane/quota/native_context_lifecycle.ts, loopx/control_plane/turn_driver/codex_context_history.py)。,unverified_scope: none),长程延续性 improved。作为严格解耦出来的 C1 核心切片,无兼容性旧包袱或账本冗余(compatibility_assessment=not_applicable,semantic_alignment=not_applicable),Python/TS 双侧核心单测、类型编译、ruff 与 premerge canary 均已实跑 PASS,同意合入。

English verdict: APPROVE

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · gpt-6.2-sol · OpenAI (runtime_reported, max)

Request changes conclusion (author-owned PR; GitHub blocks formal self-review)

REQUEST_CHANGES。有完整物理读回时能保留续跑依据。Code Mode 同轮较早的大记录会使当前有效绑定丢失。

动机 / Motivation

Codex 恢复会话时,持续任务需要找回已核验的当前条件,避免在证据完整时重复读取。

基线依赖最近工具输出与回调标识。当前提交核对物理记录和已读字节,并尝试在完整回放丢失时独立保留所选字段。

有完整物理读回时能保留续跑依据。Code Mode 同轮较早的大记录会使当前有效绑定丢失。

本次范围是 C1 原生回放和 epoch,不涉及扩大任务权限、重新批准或重做整个 #8266。

本轮较早大记录可以让后续证据完整的 Code Mode 调用失去原生回放依据。 修复原生绑定的扫描边界,重跑真实读回入口和匹配、拒绝对照。 当前 PR 只负责 Codex 原生读回与已选上下文的恢复。捕获所有权、调度准备和结算留在既有 owner。

改动思路 / Approach

TS 继续负责 epoch 和选区状态归约,Python 继续负责验证实际 rollout。当前问题出在一个父调用证明读取的记录范围,局部修复足以解决。

当前 PR 只负责 Codex 原生读回与已选上下文的恢复。捕获所有权、调度准备和结算留在既有 owner。

同一捕获的完整字节、少页和非当前输出的拒绝路径通过。经过独立选区验证的 scope 可在完整回放失败后移到新 epoch。 保留执行成功、Turn 和唯一父调用的证明,同时避免读取已结束调用之前的大正文。在原有 IO owner 内验证边界。证据不足时仍回落到原捕获读取。 同一 session、工作目录与捕获引用使用只读验证,接续执行仍由 Guard 与原结算校验决定。

具体改动 / Changes

本次核对 f3620dd,提案基线 421aa77。5 个文件 +599/−171,3 个生产文件与 2 个测试文件。 比较实验使用 421aa77,保留该版本的实际行为证据。

规范依据为 docs/architecture/rfcs/controller-executor-unified-contract-v0/contract-and-context.md,版本 421aa77。

  • C1-capture-retention (implemented)。区分保留证据、已读字节与稳定捕获。
  • C1-native-replay-coverage (not_met)。本轮较早大记录可以让后续证据完整的 Code Mode 调用失去原生回放依据。

关键代码讲解

  • _native_reader。向后寻与读回对应的本轮调用。直接调用可按 ID 完成匹配,Code Mode 还须查唯一尚未关闭的父调用。
  • _replayed_output_groups。把已观察的物理调用与保留的实际输出相交。同哈希的其他调用不能补页。
  • updateNativeContextLifecycle。独立验证的选择字段可以在完整回放失效时跨 epoch 保留。只接纳匹配摘要和作用域。

对主干的风险 / Risks

[P2] Protect replay evidence from earlier large records

本轮先产生一个 17 MiB 的正常背景事件,其后用 Code Mode 完整读回当前捕获的 15 页,再恢复同一会话。 共同基线四组对照均保留已核验候选。当前仅大记录处在本轮且使用 Code Mode 的组合丢失 native_resume,恢复后候选为空。短记录、上一轮相同大记录和直接工具调用对照均通过。 单页选区也独立复现。当前同轮 Code Mode 对照在恢复前已消费该字段,恢复后丢失该字段的保留依据,其余三组与基线四组均保留。 保留执行成功、Turn 和唯一父调用的证明,同时避免读取已结束调用之前的大正文。在原有 IO owner 内验证边界。证据不足时仍回落到原捕获读取。

验证记录。同一真实 CLI 调用和记录 fixture 在基线 4/4 通过。 定向 Python 414 通过、24 跳过,TypeScript 343 通过,控制面类型检查通过,预合并 6 项直接和 13 项选中检查通过。 原生宿主记录由隔离 fixture 提供。真实 Hook、reader 与 TS 归约均执行。样本中的文件 delta 仍可重建。实际线上频率、原生模型保留字节量和长期成本尚未测量。 与采样主干的本地合并树可生成。托管 CI 未查询。

语义与 CI 对齐

C1 接受规格的完整捕获、保留与覆盖契约。 既有捕获、resume、epoch 和选区字段。物理调用关联属于私有 IO 状态。 隔离运行 quota context-read 和 quota context-hook。交替把大背景记录放在本轮与上一轮,并比较 15 页读回后的恢复候选。 修复原生绑定的扫描边界,重跑真实读回入口和匹配、拒绝对照。

我的整体评价 / Verdict

REQUEST_CHANGES。同一捕获的完整字节、少页和非当前输出的拒绝路径通过。经过独立选区验证的 scope 可在完整回放失败后移到新 epoch。 本轮较早大记录可以让后续证据完整的 Code Mode 调用失去原生回放依据。 保留执行成功、Turn 和唯一父调用的证明,同时避免读取已结束调用之前的大正文。在原有 IO owner 内验证边界。证据不足时仍回落到原捕获读取。

原生宿主记录由隔离 fixture 提供。真实 Hook、reader 与 TS 归约均执行。样本中的文件 delta 仍可重建。实际线上频率、原生模型保留字节量和长期成本尚未测量。

English verdict: REQUEST_CHANGES — the Code Mode hook loses a fully observed native replay checkpoint when a large earlier record is in the same turn. Preserve the original binding guarantees while bounding verification to the relevant record interval.

@loopx-agent
loopx-agent force-pushed the codex/c1-standalone-core-20261118 branch from f3620dd to dca761a Compare November 18, 2026 13:03

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · Gemini · Google (self_reported)

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

已针对 PR #8942 基于最新 origin/main(648d06db54,已含 #8881、#8956、#9000、#9004)rebase 并修复 [P2] 后的精确提交 dca761a9e5e022d847ff910823d4639382fa0b79 完成完整代码走查、规范核对与隔离真实宿主验证(审查结论:APPROVE,无未解决阻塞缺陷)。

依据规范 docs/architecture/rfcs/controller-executor-unified-contract-v0.md(版本 09d0fa13594235fcd3dc13c5bd63abaf35a2d9aa)核对核心准则:

  • C1-selected-resume-epoch:经字节重放独立核验的选区在候选完整重放失败触发 context_epoch 轮换时迁延至新 epoch 并更新 read_scope 与 selected_resume,否则清空(由 tests/control_plane_ts/context_delivery.test.ts:1014-1046 与 test_codex_context_resume.py 验证)。
  • C1-physical-replay-turn-prefix:物理 rollout 绑定的 Turn 逆向扫描在已绑定开放 Code Mode exec 父调用后,通过 O(64 KiB) 流式头部分析(_reader_spans + _scan_turn_prefix_record)跳过同 Turn 更早 >16 MiB 记录正文同时严格追至 task_started 拒绝并发双开 exec 与损坏 JSON(由 14 组 test_same_turn_large_earlier_record_* 与隔离 SQLite 真实 CLI 宿主验证)。

动机 / Motivation

所有通过 loopx/control_plane/turn_driver/codex_context.py(codex_context_hook)和 loopx/control_plane/quota/native_context_lifecycle.ts(updateNativeContextLifecycle)恢复选区或原生上下文的 Codex App/CLI Agent 与 turn_driver 维护者。

修改前(f3620ddf7c 及此前 origin/main@648d06db54):当选区恢复后完整捕获重放失败触发 epoch 轮换时 updateNativeContextLifecycle 会一并清空 selected_resume 与 consumed_pointers,且 _native_reader/_reader_tail 在命中开放 Code Mode exec 父调用后仍将同 Turn 更早的 >16 MiB 记录整行拼接缓冲,导致 [P2] 同轮 17 MiB background_event 或前置大输出抛出 unsupported native reader record;修改后(dca761a9e5e022d847ff910823d4639382fa0b79):仅在 readScopeCurrent && (fileEpochCurrent || selectedResumeCurrent) 且逐项字节摘要核验一致时将 selected_resume 与已验证选区迁延到新 epoch,_reader_spans 结合 _scan_turn_prefix_record 以 (1, 64) 受限 C 正则流式校验并提取头部元数据(O(64 KiB) 内存)追至 task_started 保留并发双开 exec 拒绝,实测 159/159 恢复用例、TS 与真实 CLI 全通过。

精确 head dca761a9e5e022d847ff910823d4639382fa0b79 上:test_codex_context_resume.py 全量 159 例(含 14 例新 17 MiB 同轮/跨轮回归,tracemalloc < 2 MiB)与 4 组核心 Python 套件 344 passed in 88.17s(0 failed / 0 skipped);10 组 C1 Python 集成套件 985 passed, 14 skipped, 4 failed(4 失败已确认在未改动 origin/main@648d06db54 同现);TS 控制面 49 passed, 0 failed(706.55ms);tsc --noEmit、ruff、17 文件 mypy 全 0 错;canary premerge --tier quick ok=true(failures=0);loopx check --scan-path ok=true(errors=0);隔离 SQLite + 真实 CLI 宿主矩阵 ok=true(同轮 17 MiB Code Mode selected_resume、未变 SHA previously_delivered_fields 命中、变更 SHA 防误跳过、PreCompact 原件恢复零重入与 1 -> 2 单一扣费、错模型/缺页 2/3/篡改 SHA 拒绝全通过)。

本 PR 仅聚焦 C1 原生重放锚定、选区跨 epoch 迁延与同轮大前序流式扫描,不修改 _records 重放区间 _MAX_RECORD_BYTES 边界,亦不触碰 value-scout 在途 #8963 / #8981 / #9005 / #9008 / #9010(主干 #8956/#9004 后 test_context_first_read.py:874 与 test_saved_context_project.py:711 共 4 例失败在未改动 origin/main@648d06db54 同现,留由对应作者分支收敛,本 PR 不跨范围修改)。

改动思路 / Approach

全部生命周期迁延收口在单一 TS 所有者 updateNativeContextLifecycle,物理 rollout 解析与同轮大记录流式元数据提取限定在单一 Python IO 只读所有者 codex_context_history.py,未引入并行状态机或第三方解析依赖。

当前 PR 边界:严格收敛在 native_context_lifecycle.ts、codex_context.py、codex_context_history.py 及其 TS/Python 回归测试共 5 个文件(+1072 / -181),零新持久化字段、零新 CLI 参数、零兼容垫片。

  • 选区跨 epoch 迁延(loopx/control_plane/quota/native_context_lifecycle.ts:65-103):计算 readScopeCurrent 与 selectedResumeCurrent,在 SessionStart(source="resume") 且 candidate?.native_resume 失效触发 invalidate() 后,仅把在当前字节重放中逐项一致的 retained sections 与新 context_epoch 迁延写入 state.read_scope、state.consumed_pointers 与 state.selected_resume。
  • 物理 Turn 边界 + O(64 KiB) 流式大前序记录扫描(loopx/control_plane/turn_driver/codex_context_history.py:129-447):
    1. _reader_spans(stream, end) 以 64 KiB 逆向块扫定位每行起止偏移,消除多 MiB 拼接缓冲;
    2. parent is None 时继续严格执行 raw_len <= _MAX_RECORD_BYTES、完整 json.loads 与 _digest(record);一旦成功命中唯一开放 exec 父调用(parent is not None),对同 Turn 更早 >64 KiB 行通过 _scan_turn_prefix_record + _JsonSliceCursor(以受限重复 {1,64} 非回溯 C 正则 _JSON_STR_SPAN 跳过巨型正文,并做增量严格 UTF-8 校验及 JSON 语法检查)提取 type 与 payload 头部标识并继续向后追至 task_started(_turn_boundary)。
end ─(逆向 _reader_spans 64 KiB)─▶ item_completed(exit_code=0, turn_id) ─▶ open exec parent (parent!=None)
                                                                                  │
                 ◀──(继续扫至 task_started,拒绝同轮第 2 个未闭合 exec)─────────────┘
                 ├─ len <= 64 KiB ──▶ json.loads(raw)
                 └─ len > 64 KiB  ──▶ _scan_turn_prefix_record (C正则 {1,64} 跳正文,仅解头部元数据)

(说明:来源驱动示意图,总结 codex_context_history.py:129-447 控制流,非独立执行证据。)

具体改动 / Changes

共涉及 5 个文件(相对 origin/main@648d06db54 +1072 / -181):

  1. loopx/control_plane/quota/native_context_lifecycle.ts(+25 / -13):updateNativeContextLifecycle 支持经字节重放核验的 selected_resume 跨 epoch 轮换迁延。
  2. loopx/control_plane/turn_driver/codex_context.py(+4 / -3):在 PostToolUse 路径串联 checkpoint_native_read、observe_completed_native_reread 与 checkpoint_native_selection。
  3. loopx/control_plane/turn_driver/codex_context_history.py(+557 / -143,共 815 行):以 _reader_spans、_JsonSliceCursor、_scan_turn_prefix_record 与 _native_reader 替换旧尾扫启发式,补全 _replayed_output_groups、_replayed_selection_sections 与 observe_completed_native_reread。
  4. tests/control_plane/test_codex_context_resume.py(+451 / -21):新增 14 组 test_same_turn_large_earlier_record_*(覆盖 17 MiB 带转义及 UTF-8 background_event/closed_output、Code Mode 15+ 页与选区 tracemalloc < 2 MiB、并发双开 exec 与控制字符 fail-closed 拒绝)。
  5. tests/control_plane_ts/context_delivery.test.ts(+35 / -1):新增 updateNativeContextLifecycle 选区跨 epoch 保留与清空单测。

对主干的风险 / Risks

  • 兼容性与内存安全:所有大于 16 MiB 的活跃读取/直接回复(parent is None 或 call == reader)仍 fail-closed 拒绝;_records 重放区间继续受 _MAX_RECORD_BYTES 约束。17 MiB(445 万转义)微基准实测 _sre 峰值自 4.59 MiB 降至 147,420 B、耗时 0.461s。
  • 主干既有用例与远端 CI 说明:main@648d06db54 在 #8956/#9004 后既有 test_context_first_read.py:874(2 例)与 test_saved_context_project.py:711(2 例)失败;远端 CI 在最新提交 dca761a9e5e022d847ff910823d4639382fa0b79 排队中,合并需维护者明确授权与远端必需检查全绿。
验证项 级别 结果 实测输出(exact head dca761a9e5e022d847ff910823d4639382fa0b79)
ruff / mypy --strict / 全仓 17 文件 mypy / tsc --noEmit required PASS All checks passed! / 0 issues (17 files) / tsc exit 0
node --experimental-strip-types --test ... required PASS 49 passed, 0 failed (706.55ms)
pytest -q tests/control_plane/test_codex_context_resume.py -k large_earlier_record required PASS 14 passed, 145 deselected in 16.07s
4 组核心 Python 套件(resume, lifecycle, budget, mcp_settlement) required PASS 344 passed in 88.17s (0 failed, 0 skipped)
10 组 C1 Python 套件全扫 required MIXED (4 主干同现) 985 passed, 14 skipped, 4 failed(4 失败在未改 648d06db54 同现)
隔离 SQLite + 真实 CLI 宿主矩阵 & canary premerge & loopx check required PASS ok=true(3/3 canaries failures=0, 5 文件 errors=0, 选区恢复/拒绝/零重入单一扣费 1->2 全通过)

我的整体评价 / Verdict

  • Long-horizon 影响:improved — C1 恢复链在 epoch 轮换与同轮前序大记录场景下保持选区复用与原件恢复契约。
  • User-experience 影响:improved — 消除 Codex Code Mode 下因早期大背景/工具记录引起的静默降级全量重读。
  • visual_review:not_applicable — 纯控制面生命周期、turn_driver IO 与测试,无 UI 改动。
  • 可观测语义 / 复用 / 词表:observable_semantics 为 intentional_change_validated,repository_reuse 为 reused,authority_semantics 与 semantic_alignment 为 aligned(candidate_decision="reuse_existing")。

已完全解决 gpt-6.2-sol [P2] 阻断项并与最新 origin/main 无冲突对齐,建议进入维护者合入门禁复核。

English verdict: APPROVE exact head dca761a (author-owned COMMENTED approval review); rebased cleanly onto origin/main@648d06db54 and resolved the [P2] same-turn oversized-record binding bug in _native_reader via O(64 KiB) streaming _reader_spans and bounded {1,64} regex JSON header scanning (_scan_turn_prefix_record), verified with 344 passed Python core/budget/MCP tests (incl. 14 new 17 MiB tests), 985 passed across the 10-suite C1 sweep (4 pre-existing main failures reproduced on 648d06d), 49 passed TypeScript tests, 17-file mypy clean, quick premerge canaries (0 failures), and isolated SQLite real-CLI host continuity/recovery qualification.

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · gpt-6.2-sol · OpenAI (runtime_reported, xhigh)

Request changes conclusion (author-owned PR; GitHub blocks formal self-review)

[P2] 有效历史记录因新增的数字长度限制丢失恢复依据。 同一 Turn 里先写入约 71 KiB 的合法背景事件,随后通过 Code Mode 完整读回。base 保留 native replay checkpoint。head 在读取背景事件中一个未使用的 1,102 字节小数时拒绝整条记录,恢复失去该 checkpoint。短小数对照两边都通过。请将现有完整解码路径保留到原 16 MiB 上限,再对更长且只需元数据的前缀做有界扫描,或用等效方式保持原先可接受 JSON 的语义。保留原物理调用、唯一父调用及页覆盖检查。

动机 / Motivation

Codex 用户恢复原会话后,需要沿同一捕获继续工作,避免在完整读回已经得到验证时重复阅读。

旧 head 在同一 Turn 的早期大背景事件后丢失续跑依据。新 head 修复了已知的 17 MiB 前缀场景,但把流式解码用到了原来能完整解析的普通记录,并新增了拒绝条件。

真实 Hook 与捕获 reader 已验证原反例修复,也复现普通记录导致的回放退化。受影响的是可信续读证据,安全回退仍可取回同一捕获。

范围是原生回放和选区跨 epoch 恢复。单独的正式宿主采用、人工续跑成本与任务质量保持待验。

约71 KiB背景事件含1,102字节合法小数。base保留checkpoint,head拒绝。

改动思路 / Approach

范围是原生回放和选区跨 epoch 恢复。单独的正式宿主采用、人工续跑成本与任务质量保持待验。

TS 继续负责 epoch 和选区状态,Python 从原生历史验证物理调用和实际输出。保留原资源与权限 owner,较早记录只能参与识别当前 Turn 和唯一 Code Mode 父调用。新 parser 只需从超长前缀取得元数据。把它用于低于现有上限的普通记录,扩大了兼容风险。

在现有回放 IO owner 内,把有界扫描收敛到原 decoder 无法处理的超长前缀,同时保留唯一父调用、页覆盖和捕获来源校验。旧方案遇到 17 MiB 前缀无法确认调用。按原长度界线选用两条路径可保留普通记录,再单独改善超长前缀。

具体改动 / Changes

评审提交 dca761a9e5e022d847ff910823d4639382fa0b79,基线 648d06db542075dc023caeeb11ea368b35ef7ab7。完整差异为五个文件,+1,072/-181,涵盖三个运行时代码文件及两个测试文件。相对前次审查,主要增量是有界扫描器及其反例。

按照 docs/architecture/rfcs/controller-executor-unified-contract-v0/contract-and-context.md 在基线版本 648d06db542075dc023caeeb11ea368b35ef7ab7 的要求核对:

  • C1-capture-retention。区分捕获文件、物理读回与模型实际保留,后者未由 fixture 证明。原捕获回退仍经过既有 owner。
  • C1-native-replay-coverage。缺页、歧义及错误来源保持保守拒绝。合格普通 JSON 因无关数字字段被拒,当前未满足。
  • C1-epoch-isolation。全量回放失效可轮换 epoch,只有另外核验匹配的选区才移过去。测试覆盖保留与失效。

关键代码讲解

  1. _native_reader 反向定位匹配调用。找到 Code Mode 父调用后,超过 64 KiB 的较早记录走新扫描器。它只需要记录类型和调用元数据。
  2. _JsonSliceCursor.read_primitive 对所有 primitive 施加 1,024 字节长度上限。即使数字字段本应只跳过,skip_value 依然调用这个方法,错误会中止原本完整的读回。
  3. _replayed_output_groups 将观察到的物理记录与确切页相交,用于完整及选区检查。同字节的其他调用不能替代缺页。前向回放的记录上限仍造成一项基线也有的限制。
  4. updateNativeContextLifecycle 轮换 epoch 时只携带独立验过并匹配摘要的选区。Hook 从实际回放生成结果,TS 仍拥有复用决策。Python 调用方另收集受限的有效重新读回,供同一捕获完整性验证。

对主干的风险 / Risks

在约 71 KiB 的合法 background_event 中放入 1,102 字节小数,位于有效 Code Mode 读回之前。精确 base 与 head 都对 34 字节小数保留 checkpoint。长小数的匹配对照中,base 保留 checkpoint,head 失去 checkpoint。直接对同字节调用新 scanner 得到 oversized JSON primitive。无需改动捕获、页内容或 ID 即可触发。末端仍能通过保存文件构建 delta,因此检验应直接断言 native checkpoint,避免把文件相等误当回放成功。

另一个边界是读回结束后出现 17 MiB 合法背景事件。base 与 head 都回退到 full,同样放入 1 MiB 时两边都保留。该限制由既有前向 _records 产生,保留为现有边界。此评审的阻塞是低于原上限的新增兼容回归。合规拒绝边界保持,包括非法 JSON、歧义父调用和缺页。

独立验证的 330 项 Python 恢复与生命周期测试及 49 项 TypeScript 测试通过。14 个定向大前缀用例在 330 项中全部通过。四个追加匹配对照的 base 为 3 过、1 项基线限制,head 为 2 过、1 项相同限制及 1 项新增回归。早一次对照因历史 helper 未提供 turn_id 而失败,随后保留相同 fixture 并显式采用 fixture Turn 重跑,以上为重跑结果。标准预合并通过 6 项直接检查、5 项目录检查和 8 项风险检查。

语义与 CI 对齐

调用者仍走现有捕获与恢复 owner。无需新增 activation、权限或通用义务,也未更动模型可见指令。已知用户作用是再次查阅完整字段。按 Goal 配置本地验证,托管 CI 未查询。

我的整体评价 / Verdict

REQUEST_CHANGES。 新版在独立测试中修复旧的超大前缀反例,保留物理绑定的设计有价值。当前额外的数字限制破坏既有可恢复输入,挡住源码通过。将流式路径限制到它解决的问题,回归验证原大前缀、有效普通记录和非法历史拒绝,均可在同一个 Hook 入口完成。

工作区恢复影响长程续跑,风险是重复读回。本次没有改渲染页面。独立 fixture 执行了实际 Hook、reader 和归约,但记录为合成输入,正式客户端采用、真实用户触发频率和总体成本仍待观察。经验建议适用于核验恢复后能继续用原捕获,因此本轮检查了真实恢复入口和结果,没有把测试通过数量当作交付结果。

English verdict: REQUEST_CHANGES dca761a. The new scanner rejects a previously valid earlier record and loses a verified native replay checkpoint. The matched base and head hook runs isolate the regression.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant